using System;
using System.Drawing;
using System.Text.RegularExpressions;
using DslCrossModelFramework.Environment;

namespace DslCrossModelFramework {
    /// <summary>
    /// Abstract base class for a DSL model file
    /// </summary>
    public abstract class ModelFile : IModelFile {
        /// <summary>
        /// Gets the name of the model.
        /// </summary>
        public virtual string Name {
            get { return name; }
        }

        protected string name;

        /// <summary>
        /// Gets the contents of the DSL model file.
        /// </summary>
        /// <returns></returns>
        public abstract string GetContents();

        /// <summary>
        /// Gets the owning assembly.
        /// </summary>
        /// <value>The owning assembly.</value>
        public string OwningAssembly {
            get { return owningAssembly; }
        }

        protected string owningAssembly = string.Empty;

        /// <summary>
        /// Gets the unique model file reference.
        /// </summary>
        /// <value>The unique model file reference.</value>
        public abstract string UniqueModelFileReference { get; }

        public abstract Image GetContainerIcon();


        protected string dslPackageId;
        public string DslPackageId {
            get { return dslPackageId; }
        }

        public abstract string LocalFilePath{ get; }

        /// <summary>
        /// Gets a value indicating whether this instance is dirty.
        /// </summary>
        /// <value><c>true</c> if this instance is dirty; otherwise, <c>false</c>.</value>
        public abstract bool IsDirty { get; }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public virtual bool Equals(IModelFile other) {
            if (other == null || this.DslPackageId != other.DslPackageId) {
                return false;
            }
            return Equals(other.UniqueModelFileReference);
        }

        /// <summary>
        /// Indicates whether the current object is equal to the string.
        /// </summary>
        /// <param name="otherUniqueReference">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="otherUniqueReference"/> parameter; otherwise, false.
        /// </returns>
        public virtual bool Equals(string otherUniqueReference) {
            if (UniqueModelFileReference == otherUniqueReference) {
                return true;
            }
            foreach (DslToolsPackageInfo info in EnvironmentConfiguration.Current.Packages) {

                if (info.Guid == DslPackageId) {
                    Regex regex = new Regex(info.FileExtension + "$");
                    if (UniqueModelFileReference.EndsWith(info.FileExtension)) {
                        // Compare the model reference without the file reference with the other
                        if (regex.Replace(UniqueModelFileReference, "") == otherUniqueReference) {
                            return true;
                        }
                    }
                    else if (otherUniqueReference.EndsWith(info.FileExtension)) {
                        // Compare the other model reference without the file reference with this
                        if (regex.Replace(otherUniqueReference, "") == UniqueModelFileReference) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}